package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140"); // wht6.cn
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();

        String uid = UUID.randomUUID().toString();
        c.queueDeclare(uid, false, true, true, null);
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        // 创建队列 hello-world，如果队列已经存在，不会重复创建

        c.queueBind(uid,"logs","");

        // 从队列接收消息
        DeliverCallback deliverCallback = new DeliverCallback() {
            public void handle(String consumerTag, Delivery message) throws IOException {
                String s = new String(message.getBody());
                System.out.println("收到： "+s);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            public void handle(String consumerTag) throws IOException {
            }
        };

        // 接收消息，收到的消息会传递到一个回调对象去处理
        c.basicConsume(uid, true, deliverCallback,cancelCallback);
    }
}
